rm(List = ls())
library(dplyr)
library(readr)
library(data.table)
library(readxl)
library(stringr)
library(ggplot2)
library(cshapes)
## define a ggplot theme 
theme_nothing <- function (base_size = 12, legend = FALSE) 
{
       if (legend) {
              return(theme(line = element_blank(),rect = element_blank(), #defien the margin line
                           axis.text = element_blank(), axis.title = element_blank(), 
                           panel.background = element_blank(), panel.grid.major = element_blank(), 
                           panel.grid.minor = element_blank(), axis.ticks.length = unit(0,"cm"),
                           panel.spacing = unit(0, "lines"),
                           plot.margin = unit(c(0,  0, 0, 0), "lines"),
                           complete = TRUE))
       }
       else {
              return(theme(line = element_blank(), rect = element_blank(), 
                           text = element_blank(), axis.ticks.length = unit(0,"cm"), 
                           legend.position = "none",
                           panel.spacing = unit(0, "lines"),
                           plot.margin = unit(c(0, 0, 0, 0), "lines"), 
                           complete = TRUE))
       }
}


#### Figure 2. The Temporal Variations in Chinese Leadership Visits, 1978-2014
load("data/map_countryyear.RData")

map_ts <- map_countryyear[,c(1,4:11)]
map_ts <- map_ts %>% dplyr::group_by(year) %>% summarise_each(funs(sum))
library(reshape2)

map_ts <- melt(map_ts, id = c("year"))
library(ggrepel)
library(directlabels)
levels(map_ts$variable) <- c("Total Number of State Visit",
                             "Total Number of State Visit by China",
                             "Total Number of State Visit by Foreign Countries",
                             "Total Number of Minister Visit by China",
                             "Total Number of Minister Visit by Foreign Countries",
                             "Total Number of Visit between China and Foreign Countries",
                             "Total Number of Visit by China",
                             "Total Number of Visit by Foreign Countries")
ggplot(map_ts, aes(year, value)) +  geom_point(colour = "pink", size = 3) +  geom_line() + 
       labs(x = "Year", y = "Number of Visit",
            caption="Source: by authors") +
       facet_wrap(~variable, scales = "free",nrow = 4, ncol = 2) + theme_bw() + 
        theme(legend.position="bottom",
              legend.title=element_blank(),
              axis.text = element_text(size=11),
              text = element_text(size=11),
              strip.text.x = element_text(size = 10, color='white',
                                          angle=0),
              strip.background = element_rect(fill = "#525252", color='#525252'),
              plot.title = element_text(hjust = .5, size = 11, face = "bold"))
ggsave("visit_yearly.png", units = "cm", height =12 , width = 25)



###Figure 3. The Geographic Distribution of Chinese Leadership Visits, 1978-2014 
countrytotal <- map_countryyear[,c(2:11)]
countrytotal <- countrytotal %>% dplyr::group_by(country.name, ccode) %>% dplyr::summarise_each(funs(sum)) 
library(cshapes)
library(RColorBrewer)
library(classInt)
library(scales)
cshp2014 <- cshp(date = as.Date("2014-12-31"), useGW = FALSE)
worldmap_shp <- fortify(cshp2014, region = "COWCODE")
worldmap_shp$id <- as.integer(worldmap_shp$id)
worldmap_shp <- left_join(worldmap_shp, countrytotal, by = c("id" = "ccode"))
worldmap_shp  <- worldmap_shp[order(worldmap_shp$order), ] 


plots <- list()
##variable list
var_list <- names(worldmap_shp)[9:16]
var_listlabel <- c("Total Number of State Visit",
                   "Total Number of State Visit by China",
                   "Total Number of State Visit by Foreign Countries",
                   "Total Number of Minister Visit by China",
                   "Total Number of Minister Visit by Foreign Countries",
                   "Total Number of Visit between China and Foreign Countries",
                   "Total Number of Visit by China",
                   "Total Number of Visit by Foreign Countries")


for (i in 1:length(var_list)){
plots[[i]] <- ggplot() + geom_polygon(data=worldmap_shp, 
                             aes(x=long, y=lat, group=group, fill= worldmap_shp[,var_list[i]]),
                             color = "black",  size = 0.25) +  coord_fixed() + 
       labs(caption = paste0("Note: " ,var_listlabel[i])) +
       scale_fill_distiller(name = "# of Visit", palette = "YlGn", na.value = "grey50", guide = "colourbar",direction= 1,
                            breaks = pretty_breaks(n = 8))  +
       theme_nothing(legend = T)
}

print(plots[[1]])
##create a directory
ifelse(!dir.exists("ggplot2_figures"), dir.create("ggplot2_figures"), "Directory already exists")
##create file_names using country name and store in the above folder
file_names <- paste0("ggplot2_figures/",var_list, ".png")
##save all these plots at once inside your working directory





